Golang : Handle sub domain with Gin
Just a supplementary tutorial for the previous tutorial on how to deal with sub domain in Golang. In case you find it too troublesome like me to introduce NewServeMux just to handle sub domain, do check out Gin framework. Below is an example on how to handle sub domain with Gin framework in Golang.
This example is from previous tutorial on how to deal with query string with a minor modification.
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.Default()
router.GET("/query", func(c *gin.Context) {
first := c.Request.URL.Query().Get("first")
c.String(http.StatusOK, "First is "+first+"\n")
second := c.Request.URL.Query().Get("second")
c.String(http.StatusOK, "Second is "+second+"\n")
})
router.Run("api.example.com:8080") // port 8080 is optional //<---- here!
}
See also : Golang : Listen and Serve on sub domain example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.1k Golang : How To Use Panic and Recover
+10.6k Golang : How to delete element(data) from map ?
+5.1k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+18.7k Golang : Get download file size
+11.3k Golang : How to pipe input data to executing child process?
+13.4k Golang : Verify token from Google Authenticator App
+8.1k Golang : Append and add item in slice
+14.5k Golang : How to determine if user agent is a mobile device example
+15.6k Golang : rune literal not terminated error
+8.9k Golang : Find network service name from given port and protocol
+17.9k Golang : How to make a file read only and set it to writable again?
+7.3k Golang : How to iterate a slice without using for loop?